//
//  YXSearchBar.m
//  TestARC
//
//  Created by wuxiaoquan on 12-11-8.
//
//

#import "YXSearchBar.h"

@implementation YXSearchBar

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

- (void)layoutSubviews {
    
    [super layoutSubviews];
    
    UIButton *cancelButton = nil;
    
    for(UIView *subView in self.subviews)
    {
        if([subView isKindOfClass:[UIButton class]])
        {
            cancelButton = (UIButton*)subView;
        }
    }
    if (cancelButton)
    {
        cancelButton.clipsToBounds = YES;
        
        CGRect oldFrame = cancelButton.frame;
        float oldX = oldFrame.origin.x;
        float oldY = oldFrame.origin.y;
        float oldWidth = oldFrame.size.width;
        float oldHeight = oldFrame.size.height;
        float centerX = oldX + oldWidth/2.0f;
        float centerY = oldY + oldHeight/2.0f;
        
        float newWidth = 30.0f;
        float newHeight = 30.0f;
        
        float newX = centerX - newWidth/2.0f;
        float newY = centerY - newHeight/2.0f;
        
        //NSLog(@"x=%f,y=%f,w=%f,h=%f",newX, newY, newWidth, newHeight);
        CGRect newFrame = CGRectMake(newX, newY, newWidth, newHeight);
        
        cancelButton.frame = newFrame;
        
        // 设置圆角半径
        cancelButton.layer.masksToBounds = YES;
        cancelButton.layer.cornerRadius = 5;
        
        //还可设置边框宽度和颜色
        cancelButton.layer.borderWidth = 1;
        cancelButton.layer.borderColor = [UIColor darkGrayColor].CGColor;
        cancelButton.layer.shadowColor = [UIColor darkGrayColor].CGColor;
        
        /* For some strange reason, this code changes the font but not the text color. I assume some other internal customizations      make this not possible:
         
         UILabel *titleLabel = [cancelButton titleLabel];
         titleLabel.text = @"药";
        */
        /*
        
        // Therefore I had to create view with a label on top:
        UIView *overlay = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
        [overlay setBackgroundColor:[UIColor whiteColor]];
        [overlay setUserInteractionEnabled:NO]; // This is important for the cancel button to work
        [cancelButton addSubview:overlay];
        
        UILabel *newLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
        // Text "Cancel" should be localized for other languages
        [newLabel setText:@"药"];
        [newLabel setTextAlignment:UITextAlignmentCenter];
        // This is important for the cancel button to work
        [newLabel setUserInteractionEnabled:NO];
        [overlay addSubview:newLabel];
         */
    }
    
    
}

@end
